package cn.iocoder.yudao.module.other.service.weibo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.other.controller.admin.weibo.vo.*;
import cn.iocoder.yudao.module.other.dal.dataobject.weibo.WeiboHotSearchDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.other.dal.mysql.weibo.WeiboHotSearchMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.other.enums.ErrorCodeConstants.*;

/**
 * 微博热搜 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WeiboHotSearchServiceImpl implements WeiboHotSearchService {

    @Resource
    private WeiboHotSearchMapper weiboHotSearchMapper;

    @Override
    public Long createWeiboHotSearch(WeiboHotSearchSaveReqVO createReqVO) {
        // 插入
        WeiboHotSearchDO weiboHotSearch = BeanUtils.toBean(createReqVO, WeiboHotSearchDO.class);
        weiboHotSearchMapper.insert(weiboHotSearch);
        // 返回
        return weiboHotSearch.getId();
    }

    @Override
    public void updateWeiboHotSearch(WeiboHotSearchSaveReqVO updateReqVO) {
        // 校验存在
        validateWeiboHotSearchExists(updateReqVO.getId());
        // 更新
        WeiboHotSearchDO updateObj = BeanUtils.toBean(updateReqVO, WeiboHotSearchDO.class);
        weiboHotSearchMapper.updateById(updateObj);
    }

    @Override
    public void deleteWeiboHotSearch(Long id) {
        // 校验存在
        validateWeiboHotSearchExists(id);
        // 删除
        weiboHotSearchMapper.deleteById(id);
    }

    private void validateWeiboHotSearchExists(Long id) {
        if (weiboHotSearchMapper.selectById(id) == null) {
            throw exception(WEIBO_HOT_SEARCH_NOT_EXISTS);
        }
    }

    @Override
    public WeiboHotSearchDO getWeiboHotSearch(Long id) {
        return weiboHotSearchMapper.selectById(id);
    }

    @Override
    public PageResult<WeiboHotSearchDO> getWeiboHotSearchPage(WeiboHotSearchPageReqVO pageReqVO) {
        return weiboHotSearchMapper.selectPage(pageReqVO);
    }

}